package com.elite.controller;

import com.alibaba.fastjson.JSON;
import com.elite.entity.Seller;
import com.elite.service.SellerService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


/**
 * Created by 李帅 on 2017/1/13.
 */
@RestController
@RequestMapping("/Seller")
public class SellerController {

    @Autowired
    SellerService sellerService;
    private static final Logger LOGGER = Logger.getLogger(UserController.class);


    /**
     * 当用户从买家到卖家的时候进行认证注册
     *
     * @param sellerIdcare
     * @param sellerMail
     * @param sellerCity
     */
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public String register(String sellerIdcare, String sellerMail, String sellerCity,int userId) {
        String result;
        Seller seller = new Seller();
        Seller seller1 = sellerService.findByIdCard(sellerIdcare);
        if (seller1==null) {
            seller.setUserId(userId);
            seller.setSellerIdcare(sellerIdcare);
            seller.setSellerMail(sellerMail);
            seller.setSellerCity(sellerCity);
            seller.setSellerStatu("1");
            sellerService.addSeller(seller);
            result = "true";
        } else {
            result = "false";
        }
        return JSON.toJSON(result).toString();
    }
    @RequestMapping(value = "/registerAdm",method = RequestMethod.POST)
    public String registerAdm(String sellerIdcare, String sellerMail, String sellerCity) {
        String result;
        Seller seller = new Seller();
        Seller seller1 = sellerService.findByIdCard(sellerIdcare);
        if (seller1==null) {
            seller.setUserId(null);
            seller.setSellerIdcare(sellerIdcare);
            seller.setSellerMail(sellerMail);
            seller.setSellerCity(sellerCity);
            seller.setSellerStatu("0");
            sellerService.addSeller(seller);
            Integer id = seller.getSellerId();
            result = "admin";
        } else {
            result = "false";
        }
        return JSON.toJSON(result).toString();
    }

    @RequestMapping(value = "/LandAdm",method = RequestMethod.POST)
    public String LandAdm(String uname, String password) {
        String result;
        String admin = "admin";
        if(uname.equals(admin)&&password.equals(admin)){
            result = "11";
            return result;
        }else {
            result = "false";
            return result;
        }
    }

    @RequestMapping(value = "/test")
    public String noe(Integer userId){
        String result;
        Seller seller = null;
        Integer uId = userId;
        try {
            seller = sellerService.findByUserId(uId);
        }catch (NullPointerException e){
            e.printStackTrace();
        }
        if(seller==null){
            result="false";
            return result;
        }else {
            return JSON.toJSON(seller).toString();
        }
    }
}


